﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using oil.Models;

namespace oil.Controllers
{
    [HandleError]
    public class HomeController : Controller, IDisposable
    {
        public ActionResult Index()
        {
            var data_list = new List<GuoLuModel>();
            int row_num = 20; //每页显示的数据行数
            int page = Convert.ToInt16(Request["page"]);//显示第几页
            page = page < 1 ? 1 : page;
            int total_page = 0;
            int total_count = 0;
            using (var context = new gatewayEntities())
            {
                total_count = context.Data1.Count();
                total_page = total_count % row_num > 0 ? total_count / row_num + 1 : total_count / row_num;
                page = page <= total_page ? page : total_page;

                var data1_data = context.Data1.OrderBy(i => i.ShiJian).Skip((page - 1) * row_num).Take(row_num);

                #region 获取数据
                data_list = (from bdata in context.BaseInfo
                             join data1 in data1_data
                             on bdata.SheBei equals data1.SheBei
                             select new GuoLuModel
                             {
                                 id = bdata.id,
                                 Chang = bdata.Chang,
                                 DCWD07 = data1.DCWD07,
                                 DRWD08 = data1.DRWD08,
                                 YQHY20 = data1.YQHY20,
                                 ZQGD17 = data1.ZQGD17,
                                 ZQWD01 = data1.ZQWD01,
                                 ZQYL02 = data1.ZQYL02,
                                 ShiJian = data1.ShiJian,
                                 FSWD09 = data1.FSWD09,
                                 GBWD05 = data1.GBWD05,
                                 GFPL14 = data1.GFPL14,
                                 GouLu = data1.GuoLu,
                                 GSLL10 = data1.GSLL10,
                                 Nian = data1.Nian,
                                 PYWD03 = data1.PYWD03,
                                 Ri = data1.Ri,
                                 RQDH = data1.RQDH,
                                 RQLJ = data1.RQLJ,
                                 RYDH = data1.RYDH,
                                 RYLJ = data1.RYLJ,
                                 RYLL16 = data1.RYLL16,
                                 RYYL = data1.RYYL,
                                 SheBei = data1.SheBei,
                                 SLLJQ = data1.SLLJQ,
                                 SLLJY = data1.SLLJY,
                                 WKWD04 = data1.WKWD04,
                                 YDDH = data1.YDDH,
                                 YDLJ = data1.YDLJ,
                                 Yue = data1.Yue,
                                 Zhan = data1.Zhan,
                                 Max = bdata.Max ?? 0
                             }).ToList();
                #endregion
            }
            ViewData["action"] = "";
            ViewData["page"] = page;
            ViewData["last_page"] = (page + 10) >= total_page ? total_page : (page + 10);
            ViewData["total_page"] = total_page;
            ViewData["total_count"] = total_count;
            return View(data_list);
        }

        public ActionResult Detail()
        {
            string shebei_code = Request["code"] ?? "";
            ViewData["shebei_code"] = shebei_code;

            var zqyl_day_list = new List<object>();
            var zqwd_day_list = new List<object>();
            var zqgd_day_list = new List<object>();
            var yqhy_day_list = new List<object>();
            var zqyl_week_list = new List<object>();
            var zqwd_week_list = new List<object>();
            var zqgd_week_list = new List<object>();
            var yqhy_week_list = new List<object>();
            //获取该锅炉的24小时数据
            using (var context = new gatewayEntities())
            {
                var tempDate = DateTime.Now.AddHours(-24);
                var day_list = context.Data1.Where(i => i.ShiJian > tempDate && i.SheBei == shebei_code).OrderBy(i => i.ShiJian).ToList();
                foreach (var guolu in day_list)
                {
                    string temp_str = ((DateTime)guolu.ShiJian).Day.ToString() + "日" + ((DateTime)guolu.ShiJian).Hour + "时";
                    zqyl_day_list.Add(new { x = temp_str, y = guolu.ZQYL02 });
                    zqwd_day_list.Add(new { x = temp_str, y = guolu.ZQWD01 });
                    zqgd_day_list.Add(new { x = temp_str, y = guolu.ZQGD17 });
                    yqhy_day_list.Add(new { x = temp_str, y = guolu.YQHY20 });
                }
            }
            //获取该锅炉的一周数据
            using (var context = new gatewayEntities())
            {
                var tempDate = DateTime.Now.AddDays(-7);
                var week_list = context.Data1.Where(i => i.ShiJian > tempDate && i.SheBei == shebei_code).OrderBy(i => i.ShiJian).ToList();
                foreach (var guolu in week_list)
                {
                    string temp_str = ((DateTime)guolu.ShiJian).Month.ToString() + "月" + ((DateTime)guolu.ShiJian).Day + "日";
                    zqyl_week_list.Add(new { x = temp_str, y = guolu.ZQYL02 });
                    zqwd_week_list.Add(new { x = temp_str, y = guolu.ZQWD01 });
                    zqgd_week_list.Add(new { x = temp_str, y = guolu.ZQGD17 });
                    yqhy_week_list.Add(new { x = temp_str, y = guolu.YQHY20 });
                }
            }
            var seri = new System.Web.Script.Serialization.JavaScriptSerializer();
            
            var day_arr = new
            {
                xScale = "ordinal",
                type="line-dotted",
                yScale="linear",
                main = new List<object>()
                {
                     new { data=zqyl_day_list },
                     new { data=zqwd_day_list },
                     new { data=zqgd_day_list },
                     new { data=yqhy_day_list }
                }
            };
            var week_arr = new
            {
                xScale = "ordinal",
                type = "line-dotted",
                yScale = "linear",
                main = new List<object>()
                {
                     new { data=zqyl_week_list },
                     new { data=zqwd_week_list },
                     new { data=zqgd_week_list },
                     new { data=yqhy_week_list }
                }
            };
            ViewData["day_data"] = seri.Serialize(day_arr);
            ViewData["week_data"] = seri.Serialize(week_arr);
            ViewData["action"] = "detail_data";
            return View();
        }

        public ActionResult Reports()
        {
            return View();
        }
    }
}
